package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Spider;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("SpiderDAO")
@Transactional
public class SpiderDAOImpl extends AbstractJpaDao<Spider> implements SpiderDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Spider.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public SpiderDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Spider findSpiderById(Integer id) throws DataAccessException {

		return findSpiderById(id, -1, -1);
	}

	@Transactional
	public Spider findSpiderById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSpiderById", startResult, maxRows, id);
			return (Spider) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Spider> findSpiderByName(String name) throws DataAccessException {

		return findSpiderByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSpiderByName", startResult, maxRows, name);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByNameContaining(String name) throws DataAccessException {

		return findSpiderByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSpiderByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByTitle(String title) throws DataAccessException {

		return findSpiderByTitle(title, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByTitle(String title, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSpiderByTitle", startResult, maxRows, title);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByTitleContaining(String title) throws DataAccessException {

		return findSpiderByTitleContaining(title, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByTitleContaining(String title, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSpiderByTitleContaining", startResult, maxRows, title);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByHref(String href) throws DataAccessException {

		return findSpiderByHref(href, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByHref(String href, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSpiderByHref", startResult, maxRows, href);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByHrefContaining(String href) throws DataAccessException {

		return findSpiderByHrefContaining(href, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByHrefContaining(String href, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSpiderByHrefContaining", startResult, maxRows, href);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByTime(String time) throws DataAccessException {

		return findSpiderByTime(time, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByTime(String time, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSpiderByTime", startResult, maxRows, time);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByTimeContaining(String time) throws DataAccessException {

		return findSpiderByTimeContaining(time, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByTimeContaining(String time, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSpiderByTimeContaining", startResult, maxRows, time);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByAuthor(String author) throws DataAccessException {

		return findSpiderByAuthor(author, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByAuthor(String author, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSpiderByAuthor", startResult, maxRows, author);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByAuthorContaining(String author) throws DataAccessException {

		return findSpiderByAuthorContaining(author, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByAuthorContaining(String author, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSpiderByAuthorContaining", startResult, maxRows, author);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderBySource(String source) throws DataAccessException {

		return findSpiderBySource(source, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderBySource(String source, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSpiderBySource", startResult, maxRows, source);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderBySourceContaining(String source) throws DataAccessException {

		return findSpiderBySourceContaining(source, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderBySourceContaining(String source, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSpiderBySourceContaining", startResult, maxRows, source);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByMainContent(String mainContent) throws DataAccessException {

		return findSpiderByMainContent(mainContent, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByMainContent(String mainContent, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSpiderByMainContent", startResult, maxRows, mainContent);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByMainContentContaining(String mainContent) throws DataAccessException {

		return findSpiderByMainContentContaining(mainContent, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByMainContentContaining(String mainContent, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSpiderByMainContentContaining", startResult, maxRows, mainContent);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByTokenization(String tokenization) throws DataAccessException {

		return findSpiderByTokenization(tokenization, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByTokenization(String tokenization, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSpiderByTokenization", startResult, maxRows, tokenization);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByTokenizationContaining(String tokenization) throws DataAccessException {

		return findSpiderByTokenizationContaining(tokenization, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByTokenizationContaining(String tokenization, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSpiderByTokenizationContaining", startResult, maxRows, tokenization);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByIs_ema(Boolean is_ema) throws DataAccessException {

		return findSpiderByIs_ema(is_ema, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByIs_ema(Boolean is_ema, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSpiderByIs_ema", startResult, maxRows, is_ema);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByIs_check(Boolean is_check) throws DataAccessException {

		return findSpiderByIs_check(is_check, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByIs_check(Boolean is_check, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSpiderByIs_check", startResult, maxRows, is_check);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByExport(Boolean export) throws DataAccessException {

		return findSpiderByExport(export, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByExport(Boolean export, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSpiderByExport", startResult, maxRows, export);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByDescription(String description) throws DataAccessException {

		return findSpiderByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSpiderByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Set<Spider> findSpiderByDescriptionContaining(String description) throws DataAccessException {

		return findSpiderByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSpiderByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@Transactional
	public Spider findSpiderByPrimaryKey(Integer id) throws DataAccessException {
		return findSpiderByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Spider findSpiderByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSpiderByPrimaryKey", startResult, maxRows, id);
			return (Spider) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Spider> findSpidersByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSpidersByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Spider> findAllSpiders() throws DataAccessException {

		return findAllSpiders(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findAllSpiders(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllSpiders", startResult, maxRows);
		return new LinkedHashSet<Spider>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Spider> findSpiderByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Spider>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Spider> findSpidersByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Spider.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Spider> spiders = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return spiders;
	}

	@Transactional
	public Spider findSpiderByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findSpiderByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Spider findSpiderByPhysicalPrimaryKey(String name, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSpiderByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Spider) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Spider entity) {
		return true;
	}
}
